/*
 * Copyright © 2019-2020  Whale Cloud, Inc. All Rights Reserved.
 *
 * Notice: Whale Cloud Inc copyrights this specification.
 * No part of this specification may be reproduced in any form or means,
 * without the prior written consent of Whale Cloud Inc.
 *
 */
package com.wteam.modules.system.web;

import com.wteam.annotation.permission.PermissionGroup;
import com.wteam.domain.vo.R;
import com.wteam.modules.system.domain.Manager;
import com.wteam.modules.system.domain.dto.ManagerSubmitDto;
import com.wteam.modules.system.service.ManagerService;
import com.wteam.modules.system.domain.criteria.ManagerQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * 管理员 控制层.
 *
 * @author herry_wong
 * @since 2020-12-28
 */
@SuppressWarnings({"rawtypes"})
@Api(value = "管理员Controller", tags = {"新增：管理员操作"})
@RestController
@RequiredArgsConstructor
@RequestMapping("api/manager")
@PermissionGroup(value = "MANAGER", aliasPrefix = "管理员")
public class ManagerController {

    private final ManagerService managerService;

    //@Log("查询管理员分页")
    @ApiOperation(value = "查询管理员分页")
    @GetMapping(value = "/page")
    @PreAuthorize("@R.check('MANAGER:all', 'MANAGER:list')")
    public R getManagers(ManagerQueryCriteria criteria, Pageable pageable){
        return R.ok(managerService.queryAll(criteria, pageable));
    }

    //@Log("查询管理员详情")
    @ApiOperation(value = "查询管理员详情")
    @GetMapping(value = "/get/{uid}")
    @PreAuthorize("@R.check('MANAGER:all', 'MANAGER:list')")
    public R get(@PathVariable Long uid){
        return R.ok(managerService.findDTOById(uid));
    }

    //@Log("新增管理员")
    @ApiOperation(value = "新增管理员")
    @PostMapping(value = "/add")
    @PreAuthorize("@R.check('MANAGER:all', 'MANAGER:add')")
    public R create(@Validated @RequestBody ManagerSubmitDto resources){
        return R.ok(managerService.create(resources));
    }

    //@Log("修改管理员")
    @ApiOperation(value = "修改管理员")
    @PostMapping(value = "/edit")
    @PreAuthorize("@R.check('MANAGER:all', 'MANAGER:edit')")
    public R update(@Validated(Manager.Update.class) @RequestBody ManagerSubmitDto resources){
        managerService.update(resources);
        return R.ok();
    }

    //@Log("删除管理员")
    @ApiOperation(value = "删除管理员")
    @PostMapping(value = "/del")
    @PreAuthorize("@R.check('MANAGER:all', 'MANAGER:del')")
    public R delete(@RequestBody Set<Long> ids){
        managerService.deleteAll(ids);
        return R.ok();
    }

    //@Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@R.check('MANAGER:all', 'MANAGER:list')")
    public void download(HttpServletResponse response, ManagerQueryCriteria criteria) throws IOException {
        managerService.download(managerService.queryAll(criteria), response);
    }

}